var express = require('express');
var router = express.Router();
require("./mongoose")
//import {usermodel} from "./mongoose";
//var huomodel = mongoose.model("user",userSchema,"user");

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

//登录
router.post('/login', function(req, res, next) {
	var f = req.body;
	usermodel.find({name:f.username,pwd:f.pwd}).exec(function (err,data) {
		if (data.length == 0) {
			res.send("0")
		}else{
			//登录之后设置cookie
			//if (f.remember) {}
			res.cookie('userpwd', f);
			res.send("1")
		}
	})
  //console.log(req.body.remember,req.cookies)
});

//登录到其他页面时
router.get('/checklogin', function(req, res, next) {
  var checkcookie = req.cookies;
  if (!checkcookie.userpwd) {
  	res.send("alert('请从主页登录');location.href='login.html'");
  }else{
  	res.send("0")
  	
  }
});
//增加数据
router.post('/adduser', function(req, res) {
	var f = req.body;
	var insertuser = new usermodel();

	insertuser.name = f.name;
	insertuser.pwd = f.pwd;
	insertuser.nickname = f.nickname;
	insertuser.role = f.role;
	insertuser.department = f.department;
	insertuser.isflag = f.isflag;
	insertuser.num = parseInt(f.num);
	insertuser.save(function (err) {
		var result1
			if (err) {
				result1 = "失败"
				res.send("0");
			}else{
				result1 = "成功"
				res.send("1")
			}
			console.log(result1);
		//获取用户名
		var username = req.cookies.userpwd.username;
		var explain;
		//操作说明：explain
		if(result1 == "失败"){
			explain = username+"添加用户失败了"
		}else{
			explain = username+"添加新用户成功了"
		}
		var ctime = new Date().toLocaleString();
		var ip = req.ip;
		var loginsert = new logmodel();
		/*
		username:String,
			explain:String,
			ctime:String,
			result:String,
			ip:String
		* */
		loginsert.username = username;
		loginsert.explain = explain;
		loginsert.ctime = ctime;
		loginsert.ip = ip;
		loginsert.result = result1;
		loginsert.save();
		// console.log(loginsert )
	})
  	
});
//查询数据,渲染首页
router.post("/edituser",function (req,res) {
	var obj = req.body;
	//name,pwd,nickname,role,department,isflag,num,edid
	usermodel.findById(obj.edid).exec(function (err,data) {
	  	data.name = obj.name;
	  	data.pwd = obj.pwd;
	  	data.nickname = obj.nickname;
	  	data.role = obj.role;
	  	data.department = obj.department;
	  	data.isflag = parseInt( obj.isflag );
	  	data.num = obj.num;
	  	data.save(function (err) {
	  		if (err) {
	  			res.send("0")
	  		}else{
	  			res.send("1")
	  		}
	  	})
	})
})


//删除数据
router.get('/deletemore', function(req, res) {
  var ids = req.query.data;
 console.log(ids)
  //遍历删除数据
  for (let i = 0; i < ids.length; i++) {
  	usermodel.findById(ids[i]).exec(function (err,data) {
	  	data.remove(function (err) {	
			  if(err){
				  res.send("0")
			  }	
	  	})
	  })
  }
 res.send("1")
});

//将id对应的值传入前端网页
router.get('/useredit', function(req, res) {
  var id = req.query.str;
  usermodel.findById(id).exec(function (err,data) {
  	res.send(data)
  })
  
});

//查询搜索功能
router.get('/userlist', function(req, res) {
  var datas = req.query;
  var obj = {};
  if (datas.s_parment != "全部") {
  	obj.department = datas.s_parment;
  }
  if (datas.s_role != "全部") {
  	obj.role = datas.s_role;
  }
  if (datas.s_name != "") {
  	var reg = new RegExp(datas.s_name);//构建模糊查询的条件
  	obj.name = reg;
  }
  
  /*
   根据前台发送过来的数据，进行分页
   1. 默认每一页的条数
   2. 查询数据
   	skip()  limit()
   * */
  var page = datas.n;//当前页码
  var pagenum = 3;//每一页显示的条数
  var skipnum = (page-1)*pagenum;

  
  usermodel.find(obj).skip(skipnum).limit(pagenum).exec(function (err,data) {
  	res.send(data)
  })
});
//获取总条数
router.get('/count', function(req, res) {
	usermodel.find({}).exec(function (err,data) {
  	var count = data.length;//获取总条数
  	
  	res.send({count})
  })
})
//修改密码
router.get('/pwddit', function(req, res) {
	var pwds = req.query;
	// console.log(pwds)
	var username = req.cookies.userpwd.username;
	// console.log(username)
 	// console.log(pwds.password1,username)
 	usermodel.find({name:username,pwd:pwds.password1}).exec(function (err,data) {
 		//data是一个数组----注意数据类型
 		data[0].pwd = pwds.password3;
 		data[0].save(function (err) {
 			res.send("1001")
 		})
 	})
});


router.get("/log",function(req,res){
	logmodel.find({}).exec(function(err,data){
		// console.log(data)
		res.send(data)
	})
})



module.exports = router;
